package ar.uba.fi.taller2.TreesWeb.ui.initiator;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.util.Initiator;

import ar.uba.fi.taller2.TreesWeb.Parameters;
import ar.uba.fi.taller2.TreesWeb.ui.Lookup;

public class ParametersInit implements Initiator {
	
	private static HashMap lookups = null;
	
	public void doAfterCompose(Page arg0) throws Exception {
		// TODO Auto-generated method stub

	}

	public boolean doCatch(Throwable arg0) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public void doFinally() throws Exception {
		// TODO Auto-generated method stub

	}

	public void doInit(Page arg0, Object[] arg1) throws Exception
	{
		arg0.setVariable((String) arg1[0], getLookups(  ) );
	}
	
	public static List getLookups(  ) throws  IOException
	{
		
		if (lookups == null)
		{
			List params = new ArrayList();
			lookups = new HashMap();
			BufferedReader input =  new BufferedReader(new FileReader(Parameters.getParametros().getPath()+Parameters.getParametros().getName()));			
			String line;
			while( ( line = input.readLine()) != null )
				{
					String[] data= line.split("["+'\u0009'+"]");
					if (data.length == 3){
						String type = data[0];
						String clave = data[1];
						String valor = data[2];
						Lookup lookup = new Lookup("parametros", type, valor);
						params.add( lookup );
					}					
				}
			lookups.put("parametros", params);
			}		
		return (List)lookups.get("parametros");
	}

}
